
package GUI;

import Klijent.Komunikacija;
import Server.Korisnik;
import Klijent.Clipboard;
import Strukture.Flare;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;


/**
 *
 * @author igor
 */
public class NalogForma extends javax.swing.JFrame{

    /**
     * Creates new form NalogForma
     */
    public NalogForma() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldIme = new javax.swing.JTextField();
        jTextFieldPrezime = new javax.swing.JTextField();
        jTextFieldKorIme = new javax.swing.JTextField();
        jTextFieldMail = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPasswordFieldSifra = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jTextFieldIme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldImeActionPerformed(evt);
            }
        });

        jButton1.setText("Kreiraj nalog");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Ime");

        jLabel2.setText("Prezime");

        jLabel3.setText("Korisnicko ime");

        jLabel4.setText("Sifra");

        jLabel5.setText("E-majl adresa");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(68, 253, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldIme)
                            .addComponent(jTextFieldPrezime)
                            .addComponent(jTextFieldKorIme)
                            .addComponent(jTextFieldMail)
                            .addComponent(jPasswordFieldSifra, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldIme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPrezime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldKorIme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jPasswordFieldSifra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    public void flare (String tekst){
        JOptionPane.showMessageDialog(this, tekst);
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        NalogForma nalog = new NalogForma();
        Flare raketa = new Flare (null);
        
        String ime = jTextFieldIme.getText();
        String prezime = jTextFieldPrezime.getText();
        String korIme = jTextFieldKorIme.getText();
        char[] sifra = jPasswordFieldSifra.getPassword();
        String sifraStr = new String(sifra);
        String mail = jTextFieldMail.getText();
        
        if(nalog.proveriDaLiJePrazno(ime, prezime, korIme, sifraStr, mail) == false)
//            if(nalog.proveriDaLiPostojiIstoKorIme() == true){
//                JOptionPane.showMessageDialog(this, "Korisnicko ime vec postoji, unesitie drugo korisnicko ime.");
//                return;
//            }else{
//            nalog.napraviNoviFajl();
//            }
        dispose();
        Korisnik korisnikIlija = new Korisnik(ime, sifraStr);
        try {
            //parametar 1 znaci da pozivamo deo metode koji radi pravljenje novog naloga
            Komunikacija.communication(korisnikIlija, 1,null, raketa);
 
        } catch (Exception ex) {
            Logger.getLogger(NalogForma.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("USPELOOOOOOO");
        JOptionPane.showMessageDialog(this, raketa.tekstZaPrenos);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextFieldImeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldImeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldImeActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }//GEN-LAST:event_formWindowClosing
    /**
     * metoda koja proverava ako korisnik nije popunio sva polja i opominje ga da sva polja 
     * moraju biti popunjena
     * @return false oznacava da polja nisu prazna a true da jesu...
     */
    public boolean proveriDaLiJePrazno(String ime, String prezime, String korIme, String sifraStr, String mail){
//        String ime = jTextFieldIme.getText();
//        String prezime = jTextFieldPrezime.getText();
//        String korIme = jTextFieldKorIme.getText();
//        char[] sifra = jPasswordFieldSifra.getPassword();
//        String sifraStr = new String(sifra);
//        String mail = jTextFieldMail.getText();
        
        
        //trim() se poziva da bi se onemogucio korisnik koji zaspi na tastaturi da napise: "                 "
        if (ime.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Polje ime ne sme da bude prazno");
                return true;
        }
        if (prezime.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Polje prezime ne sme da bude prazno");
                return true;
        }
        if (korIme.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Polje korisnicko ime ne sme da bude prazno");
                return true;
        }
        if (sifraStr.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Polje sifra ne sme da bude prazno");
                return true;
        }
        if (mail.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Polje E-mail adresa ne sme da bude prazno");
                return true;
        }
        return false;
        
    }
    /**
     * Metoda koja proverava da li vec postoji korisnicko ime koje je korisnik uneo i opominje ga 
     * da unese drugo korisnicko ime.
     * @return ukoliko vrati false, ne postoji isto korisnicko ime, 
     *         u suprotnom potrebno je promeniti korisnicko ime
     */
    public boolean proveriDaLiPostojiIstoKorIme(){
        
        String imeFajla = jTextFieldKorIme.getText()+".txt";
        
        File[] roots = File.listRoots();
        for(int i = 0; i<roots.length; i++){ 
            File[] files = new File(roots[i].toString()).listFiles();
                for(int j = 1; j<files.length;j++){
                    if(files[i].toString().equals(imeFajla)){
                        JOptionPane.showMessageDialog(this, "Vec postoji korisnicko ime, molimo unesite drugo korisnicko ime");
                        return true;
                    }
                  }
                }
        return false;
    }
    /**
     * metoda pravi novi fajl i u njega unosi sve neophodne podatke.
     */
//    public void napraviNoviFajl(){
//        String ime = jTextFieldIme.getText();
//        String prezime = jTextFieldPrezime.getText();
//        String korIme = jTextFieldKorIme.getText();
//        char[] sifra = jPasswordFieldSifra.getPassword();
//        String sifraStr = new String(sifra);
//        String mail = jTextFieldMail.getText();
//        String sve = korIme+" "+sifraStr+" "+ime+" "+prezime+" "+mail+" ";
//        Korisnik user = new Korisnik();
//        user.setNoviNalog(sve);
//        user.setIme(korIme);
//        user.setID('0');
//        
//        String fajl = jTextFieldKorIme.getText()+".txt";
//        try{
//            PrintWriter out = new PrintWriter(new FileWriter(fajl));
//            out.print(sve);
//            out.close();
//        }catch(Exception e){
//            JOptionPane.showMessageDialog(this, "Greska: "+e.getMessage());
//        }
//        JOptionPane.showMessageDialog(this, "Novi nalog je kreiran!");
//    }
    
    /**
     * @param args the command line arguments
     */
    public static void OtvoriNalogFormu() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NalogForma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NalogForma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NalogForma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NalogForma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NalogForma().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField jPasswordFieldSifra;
    private javax.swing.JTextField jTextFieldIme;
    private javax.swing.JTextField jTextFieldKorIme;
    private javax.swing.JTextField jTextFieldMail;
    private javax.swing.JTextField jTextFieldPrezime;
    // End of variables declaration//GEN-END:variables
}
